<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
</head>
<body>
    <p>数值1 <input type="text" id="n1"></p>
    <p>数值2 <input type="text" id="n2"></p>
    <p>
        <button onclick="count('+')">相加</button>
        <button onclick="count('-')">相减</button>
        <button onclick="count('*')">相乘</button>
        <button onclick="count('/')">相除</button>
    </p>
    <p>结果：<input type="text" id="res"></p>


    <script>
        function count(sum){
           var n1 = document.querySelector("#n1").value;
           var n2 = document.querySelector("#n2").value;
            n1 = Number(n1);
            n2 = Number(n2);
            var res = 0;
            switch (sum){
                case '+':
                    res = n1+n2;
                    break;
                case '-':
                    res = n1-n2;
                    break;
                case '*':
                    res = n1*n2;
                    break;
                case '/':
                    res = n1/n2;
                    break;

            }
            document.querySelector("#res").value = res;
        }
    </script>
</body>
</html>